package system.service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import system.common.Arguments;
import system.common.Constants;
import system.dao.DB1_WebkeeperMigrateDao;
import system.model.DB1_WebkeeperDto;
import system.util.DateUtil;
import system.util.FileUtil;

@Service
public class DB1_WebkeeperMigrateService {
	@Autowired
	private DB1_WebkeeperMigrateDao db1_WebkeeperMigrateDao;

	private static Logger _log = LoggerFactory.getLogger(DB1_WebkeeperMigrateService.class);
	private static final String _LogPrefixStr = "test Log> ";
	
	public void webkeeper_migrate_01(Arguments arguments) throws ParseException, IOException {
		String fileNameSuffix = arguments.getParameter1() + "_" + arguments.getFileName1() + "_" + arguments.getFileName2();
		String historyFileName = Constants.WEBKEEPER_MIGRATE_01_History + Constants.WEBKEEPER_MIGRATE_01 + Constants.fileNamePrefix + fileNameSuffix;
		String currentFileName = Constants.WEBKEEPER_MIGRATE_01_Current + Constants.WEBKEEPER_MIGRATE_01;
		String dbName = "WK_LOG_" + arguments.getDBName();
		String tableName1 = "TB_LOG_HTTP_emd27w0_" + arguments.getDBName();
		String tableName2 = "TB_LOG_USETIME_e	md27w0_" + arguments.getDBName();
		arguments.setDBFullName(dbName);
		System.out.println(">> dbName :: " + dbName);
		
		// check for directory
		FileUtil.directoryCheck(Constants.WEBKEEPER_MIGRATE_01_History);
		FileUtil.directoryCheck(Constants.WEBKEEPER_MIGRATE_01_Current);
		
		// delete to current file
		FileUtil.deleteFile(Constants.WEBKEEPER_MIGRATE_01, Constants.WEBKEEPER_MIGRATE_01_Current);
		
		int time;
		String tableTimeName;
		String stepNumber;
		long start;
		long end;
		float rsTime;
		
		File file = null;
		BufferedWriter bufferedWriter = null;
		Constants.rsTime = 0;
		Constants.fileMakeTime = 0;
		Constants.fileCopyTime = 0;
		Constants.totalRecord = 0;
		
		List<DB1_WebkeeperDto> webkeeper_migrate_01_list;

		String startTime = arguments.getParameter2();
		String endTime = arguments.getParameter3();
		
		if (arguments.getParameter1().equals("DAY")) {
			String tempStartTime = startTime;
			String tempEndTime;
			int roofCount = 48 * arguments.getDurationValue();
			
			Date d;
			DateUtil.setDefaultDateFormat("yyyyMMddHHmm");
			for (int i = 1; i <= roofCount; i++) {
				d = Constants.simDf_Min.parse(startTime);
				tempEndTime = DateUtil.dateAddFormat(Calendar.MINUTE, (i * 30), d);
				arguments.setParameter2(tempStartTime, 1);
				if (i == roofCount) {
					arguments.setParameter3(endTime, 1);
				} else {
					arguments.setParameter3(tempEndTime, 1);
				}
				// rs start
				start = System.currentTimeMillis();

				time = Integer.parseInt(arguments.getParameter2().substring(11, 13));
				if (time % 2 != 0) {
					time = time - 1;
				}
				tableTimeName = "0" + time;
				arguments.setTableName1(tableName1 + "_" + tableTimeName.substring((tableTimeName.length() - 2), tableTimeName.length()));
				arguments.setTableName2(tableName2 + "_" + tableTimeName.substring((tableTimeName.length() - 2), tableTimeName.length()));
				
				webkeeper_migrate_01_list = db1_WebkeeperMigrateDao.webkeeper_migrate_01_list(arguments);
				Constants.totalRecord += webkeeper_migrate_01_list.size();

				end = System.currentTimeMillis();
				rsTime = (end - start) / 1000f;
				Constants.rsTime = Constants.rsTime + rsTime;
				// rs end
				// file-make start
				start = System.currentTimeMillis();
				
				stepNumber = "000" + i;
				file = new File(historyFileName + "_" + stepNumber.substring((stepNumber.length() - 4), stepNumber.length()) + FileUtil.genFileNumber(0) + Constants.fileNameExtension);
				bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));

				ExportService exportService = new ExportService();
				exportService.OutputList(webkeeper_migrate_01_list, bufferedWriter, historyFileName, stepNumber.substring((stepNumber.length() - 4), stepNumber.length()), 0, Constants.WEBKEEPER_MIGRATE_01);
				
				end = System.currentTimeMillis();
				Constants.fileMakeTime = (end - start) / 1000f;
				// file-make end
				
				_log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
				_log.debug(_LogPrefixStr + "|" + " step-" + i + " :: record(" + webkeeper_migrate_01_list.size() + "), rs(" + rsTime + "s)");
				_log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
				
				d = Constants.simDf_Min.parse(tempEndTime);
				tempStartTime = DateUtil.dateAddFormat(Calendar.MINUTE, 1, d);
			}
			// file-copy start
			start = System.currentTimeMillis();
			
			FileUtil.copyBytes(Constants.WEBKEEPER_MIGRATE_01_History, Constants.WEBKEEPER_MIGRATE_01 + Constants.fileNamePrefix + fileNameSuffix, currentFileName, Constants.fileNameExtension);

			end = System.currentTimeMillis();
			Constants.fileCopyTime = (end - start) / 1000f;
			// file-copy end
		} else {
			// rs start
			start = System.currentTimeMillis();
			
			time = Integer.parseInt(arguments.getParameter2().substring(11, 13));
			if (time % 2 != 0) {
				time = time - 1;
			}
			tableTimeName = "0" + time;
			arguments.setTableName1(tableName1 + "_" + tableTimeName.substring((tableTimeName.length() - 2), tableTimeName.length()));
			arguments.setTableName2(tableName2 + "_" + tableTimeName.substring((tableTimeName.length() - 2), tableTimeName.length()));
			
			webkeeper_migrate_01_list = db1_WebkeeperMigrateDao.webkeeper_migrate_01_list(arguments);
			Constants.totalRecord = webkeeper_migrate_01_list.size();

			end = System.currentTimeMillis();
			Constants.rsTime = (end - start) / 1000f;
			// rs end
			// file-make start
			start = System.currentTimeMillis();
			
			file = new File(historyFileName + "_0001" + FileUtil.genFileNumber(0) + Constants.fileNameExtension);
			bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));

			ExportService exportService = new ExportService();
			exportService.OutputList(webkeeper_migrate_01_list, bufferedWriter, historyFileName, "_0001", 0, Constants.WEBKEEPER_MIGRATE_01);
			
			end = System.currentTimeMillis();
			Constants.fileMakeTime = (end - start) / 1000f;
			// file-make end
			// file-copy start
			start = System.currentTimeMillis();
			
			FileUtil.copyBytes(Constants.WEBKEEPER_MIGRATE_01_History, Constants.WEBKEEPER_MIGRATE_01 + Constants.fileNamePrefix + fileNameSuffix, currentFileName, Constants.fileNameExtension);

			end = System.currentTimeMillis();
			Constants.fileCopyTime = (end - start) / 1000f;
			// file-copy end
		}
	}
	
	public void webkeeper_migrate_02(Arguments arguments) throws IOException, ParseException {
		String fileNameSuffix = arguments.getParameter1() + "_" + arguments.getFileName1() + "_" + arguments.getFileName2();
		String historyFileName = Constants.WEBKEEPER_MIGRATE_02_History + Constants.WEBKEEPER_MIGRATE_02 + Constants.fileNamePrefix + fileNameSuffix;
		String currentFileName = Constants.WEBKEEPER_MIGRATE_02_Current + Constants.WEBKEEPER_MIGRATE_02;
		String dbName = "WK_LOG_" + arguments.getDBName();
		String tableName1 = "TB_LOG_HTTP_emd27w0_" + arguments.getDBName();
		String tableName2 = "TB_LOG_USETIME_emd27w0_" + arguments.getDBName();
		arguments.setDBFullName(dbName);
		System.out.println(">> dbName :: " + dbName);
		
		// check for directory
		FileUtil.directoryCheck(Constants.WEBKEEPER_MIGRATE_02_History);
		FileUtil.directoryCheck(Constants.WEBKEEPER_MIGRATE_02_Current);
		
		// delete to current file
		FileUtil.deleteFile(Constants.WEBKEEPER_MIGRATE_02, Constants.WEBKEEPER_MIGRATE_02_Current);
		
		int time;
		String tableTimeName;
		String fileNumber;
		long start;
		long end;
		float rsTime;
		
		File file = null;
		BufferedWriter bufferedWriter = null;
		Constants.rsTime = 0;
		Constants.fileMakeTime = 0;
		Constants.fileCopyTime = 0;
		Constants.totalRecord = 0;
		
		List<DB1_WebkeeperDto> webkeeper_migrate_02_list;

		String startTime = arguments.getParameter2();
		String endTime = arguments.getParameter3();
		
		if (arguments.getParameter1().equals("DAY")) {
			String tempStartTime = startTime;
			String tempEndTime;
			int roofCount = 48 * arguments.getDurationValue();
			
			Date d;
			DateUtil.setDefaultDateFormat("yyyyMMddHHmm");
			for (int i = 1; i <= roofCount; i++) {
				d = Constants.simDf_Min.parse(startTime);
				tempEndTime = DateUtil.dateAddFormat(Calendar.MINUTE, (i * 30), d);
				arguments.setParameter2(tempStartTime, 1);
				if (i == roofCount) {
					arguments.setParameter3(endTime, 1);
				} else {
					arguments.setParameter3(tempEndTime, 1);
				}
				// rs start
				start = System.currentTimeMillis();

				time = Integer.parseInt(arguments.getParameter2().substring(11, 13));
				if (time % 2 != 0) {
					time = time - 1;
				}
				tableTimeName = "0" + time;
				arguments.setTableName1(tableName1 + "_" + tableTimeName.substring((tableTimeName.length() - 2), tableTimeName.length()));
				arguments.setTableName2(tableName2 + "_" + tableTimeName.substring((tableTimeName.length() - 2), tableTimeName.length()));
				
				webkeeper_migrate_02_list = db1_WebkeeperMigrateDao.webkeeper_migrate_02_list(arguments);
				Constants.totalRecord += webkeeper_migrate_02_list.size();

				end = System.currentTimeMillis();
				rsTime = (end - start) / 1000f;
				Constants.rsTime = Constants.rsTime + rsTime;
				// rs end
				// file-make start
				start = System.currentTimeMillis();
				
				fileNumber = "000" + i;
				file = new File(historyFileName + "_" + fileNumber.substring((fileNumber.length() - 4), fileNumber.length()) + FileUtil.genFileNumber(0) + Constants.fileNameExtension);
				bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));

				ExportService exportService = new ExportService();
				exportService.OutputList(webkeeper_migrate_02_list, bufferedWriter, historyFileName, "_" + fileNumber.substring((fileNumber.length() - 4), fileNumber.length()), 0, Constants.WEBKEEPER_MIGRATE_02);
				
				end = System.currentTimeMillis();
				Constants.fileMakeTime = (end - start) / 1000f;
				// file-make end
				// file-copy start
				start = System.currentTimeMillis();
				
				FileUtil.copyBytes(Constants.WEBKEEPER_MIGRATE_02_History, Constants.WEBKEEPER_MIGRATE_02 + Constants.fileNamePrefix + fileNameSuffix, currentFileName, Constants.fileNameExtension);

				end = System.currentTimeMillis();
				Constants.fileCopyTime = (end - start) / 1000f;
				// file-copy end
				_log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
				_log.debug(_LogPrefixStr + "|" + " step-" + i + " :: record(" + webkeeper_migrate_02_list.size() + "), rs(" + rsTime + "s)");
				_log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
				
				d = Constants.simDf_Min.parse(tempEndTime);
				tempStartTime = DateUtil.dateAddFormat(Calendar.MINUTE, 1, d);
			}
		} else {
			// rs start
			start = System.currentTimeMillis();
			
			time = Integer.parseInt(arguments.getParameter2().substring(11, 13));
			if (time % 2 != 0) {
				time = time - 1;
			}
			tableTimeName = "0" + time;
			arguments.setTableName1(tableName1 + "_" + tableTimeName.substring((tableTimeName.length() - 2), tableTimeName.length()));
			arguments.setTableName2(tableName2 + "_" + tableTimeName.substring((tableTimeName.length() - 2), tableTimeName.length()));
			
			webkeeper_migrate_02_list = db1_WebkeeperMigrateDao.webkeeper_migrate_02_list(arguments);
			Constants.totalRecord = webkeeper_migrate_02_list.size();

			end = System.currentTimeMillis();
			Constants.rsTime = (end - start) / 1000f;
			// rs end
			// file-make start
			start = System.currentTimeMillis();
			
			file = new File(historyFileName + "_0001" + FileUtil.genFileNumber(0) + Constants.fileNameExtension);
			bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));

			ExportService exportService = new ExportService();
			exportService.OutputList(webkeeper_migrate_02_list, bufferedWriter, historyFileName, "_0001", 0, Constants.WEBKEEPER_MIGRATE_02);
			
			end = System.currentTimeMillis();
			Constants.fileMakeTime = (end - start) / 1000f;
			// file-make end
			// file-copy start
			start = System.currentTimeMillis();
			
			FileUtil.copyBytes(Constants.WEBKEEPER_MIGRATE_02_History, Constants.WEBKEEPER_MIGRATE_02 + Constants.fileNamePrefix + fileNameSuffix, currentFileName, Constants.fileNameExtension);

			end = System.currentTimeMillis();
			Constants.fileCopyTime = (end - start) / 1000f;
			// file-copy end
		}
	}
}
